package com.gzyj.server.utils;

import cn.hutool.core.lang.Pair;
import org.apache.commons.lang3.tuple.MutableTriple;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lhj
 */
public class TimeUtils {

    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat datesdf = new SimpleDateFormat("yyyy-MM-dd");

    public static Date MIN_VALUE() {
        return new Date(0);
    }

    public static String getNow() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
    public static String getToday() {
        SimpleDateFormat datesdf = new SimpleDateFormat("yyyy-MM-dd");
        return datesdf.format(new Date());
    }

    public static Date toDate(String d) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(d);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String date2Str(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(d);

    }

    public static long getHour(String str){
        SimpleDateFormat HOUR = new SimpleDateFormat("HH:mm:ss");
        String[] time= str.split(" ");

        try {
            if(time.length>1){
                return HOUR.parse(time[1]).getTime()/1000;
            }else{
                return HOUR.parse(time[0]).getTime()/1000;
            }


        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    public static Long getMinusSecond(Date date1, String date2) {
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return getMinusSecond(date1, sdf.parse(date2));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Long getMinusSecond(String date1, Date date2) {
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return getMinusSecond(sdf.parse(date1), date2);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Long getMinusSecond(Date date1, Date date2) {

        Long d = 0L;
        d = (date1.getTime() - date2.getTime()) / 1000;
        return d;
    }


    public static Pair<Boolean, Integer> CheckData(Date oldGpsTime, String newGpsTime) {
        int flag;
        //判断位置数据GPS时间是否与系统相差30分钟、
        Long ts = getMinusSecond(new Date(), newGpsTime);
        if (Math.abs(ts) > 60 * 60) {
            flag = -1;
            return new Pair<>(false, flag);
        }
        if (oldGpsTime != null) {
            //判断位置数据GPS时间是否比记录的最后位置时间小，小则错误。
            Long tsCompare = getMinusSecond(newGpsTime, oldGpsTime);
            if (tsCompare <= 0) {
                flag = -2;
                return new Pair<>(false, flag);
            }
            flag = 0;
            return new Pair<>(true, flag);
        } else {
            flag = 1;
            //无数据时候无法验证，返回验证通过
            return new Pair<>(true, flag);
        }
    }

    private final static SimpleDateFormat HOUR = new SimpleDateFormat("HH:mm:ss");

    /// <summary>
    /// 判断时间是否在某个时间范围内
    /// </summary>
    public static boolean InTimeRange(String gpsTime, String sTime, String eTime) {
        try {
            SimpleDateFormat HOUR = new SimpleDateFormat("HH:mm:ss");
            Date tsGpsTime = HOUR.parse(gpsTime);
            Date tsBeginTime = HOUR.parse(sTime);
            Date tsEndTime = HOUR.parse(eTime);
            //跨零点
            if (tsBeginTime.after(tsEndTime)) {
                if (tsGpsTime.after(tsBeginTime) && tsGpsTime.before(HOUR.parse("23:59:59")))
                    return true;
                else if (tsGpsTime.after(HOUR.parse("00:00:00")) && tsGpsTime.before(tsEndTime))
                    return true;
                else
                    return false;
            } else {
                return (tsGpsTime.after(tsBeginTime) && tsGpsTime.before(tsEndTime)) ? true : false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public static boolean InTimeRange(Date gpsTime, String sTime, String eTime) {
        try {
            SimpleDateFormat HOUR = new SimpleDateFormat("HH:mm:ss");
            Date tsGpsTime = HOUR.parse(HOUR.format(gpsTime));
            Date tsBeginTime = HOUR.parse(sTime);
            Date tsEndTime = HOUR.parse(eTime);
            //跨零点
            if (tsBeginTime.after(tsEndTime)) {
                if (tsGpsTime.after(tsBeginTime) && tsGpsTime.before(HOUR.parse("23:59:59")))
                    return true;
                else if (tsGpsTime.after(HOUR.parse("00:00:00")) && tsGpsTime.before(tsEndTime))
                    return true;
                else
                    return false;
            } else {
                return (tsGpsTime.after(tsBeginTime) && tsGpsTime.before(tsEndTime)) ? true : false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    private static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }


    /// <summary>
    /// endTime必须>=begintime,否则计算错误。
    /// sTime必须>=eTime 表示跨天，否则计算错误。
    /// </summary>
    /// <param name="beginTime"></param>
    /// <param name="endTime"></param>
    /// <param name="sTime"></param>
    /// <param name="eTime"></param>
    /// <returns></returns>
    public static MutableTriple<Boolean, Double, Double> CalcFatigueTime(Date beginTime, Date endTime, String sTime, String eTime) {
        double baitian;
        double yejian;

        try {
            SimpleDateFormat HOUR = new SimpleDateFormat("HH:mm:ss");
            baitian = 0;
            yejian = 0;
            if (getMinusSecond(beginTime, endTime) > 0) {
                return MutableTriple.of(false, baitian, yejian);
            }

            Date ts_bTime = HOUR.parse(HOUR.format(beginTime));
            Date ts_eTime = HOUR.parse(HOUR.format(endTime));
            Date ts_set_bTime = HOUR.parse(sTime);
            Date ts_set_eTime = HOUR.parse(eTime);

            boolean b_inNight = InTimeRange(beginTime, sTime, eTime);
            boolean e_inNight = InTimeRange(endTime, sTime, eTime);

            double second = 1;

            int count = differentDays(beginTime,endTime);
            if (count == 0) {
                //开始和结束时间没有跨天
                if (b_inNight) {
                    //开始时间在夜间
                    if (ts_bTime.getTime() >= ts_set_bTime.getTime() && ts_bTime.getTime() <= HOUR.parse("23:59:59").getTime()) {
                        //开始时间在夜间 22:00-00:00
                        if (e_inNight) {
                            //开始时间在夜间 22:00-00:00 - 结束时间在夜间
                            baitian = 0;
                            yejian = (ts_eTime.getTime() - ts_bTime.getTime())/1000;
                        } else {
                            //开始时间在夜间 22:00-00:00 - 结束时间在白天
                            //结束时间比开始时间大 不可能进入
                            baitian = 0;
                            yejian = 0;
                        }
                    } else if (ts_bTime.getTime() >= HOUR.parse("00:00:00").getTime() && ts_bTime.getTime() <= ts_set_eTime.getTime()) {
                        //开始时间在夜间 00:00-05:00
                        if (e_inNight) {
                            //开始时间在夜间 00:00-05:00 - 结束时间在夜间
                            if (ts_eTime.getTime() >= ts_set_bTime.getTime() && ts_eTime.getTime() <= HOUR.parse("23:59:59").getTime()) {
                                //开始时间在夜间 00:00-05:00 - 结束时间在夜间 22:00 - 00:00
                                baitian = (ts_set_bTime.getTime() - ts_set_eTime.getTime())/1000;
                                yejian = (ts_eTime.getTime() - ts_set_bTime.getTime())/1000;
                                yejian += (ts_set_eTime.getTime() - ts_bTime.getTime())/1000;
                            } else if (ts_eTime.getTime() >= HOUR.parse("00:00:00").getTime() && ts_eTime.getTime() <= ts_set_eTime.getTime()) {
                                //开始时间在夜间 00:00-05:00 - 结束时间在夜间 00:00 - 05:00
                                baitian = 0;
                                yejian = (ts_eTime.getTime() - ts_bTime.getTime())/1000;
                            }
                        } else {
                            //开始时间在夜间 00:00-05:00 - 结束时间在白天
                            baitian = (ts_eTime.getTime() - ts_set_eTime.getTime())/1000;
                            yejian = (ts_set_eTime.getTime() - ts_bTime.getTime())/1000;
                        }
                    }
                } else {
                    //开始时间在白天
                    if (e_inNight) {
                        //开始时间在白天 - 结束时间在夜间，夜间必定是在 22:00 - 00:00
                        baitian = (ts_set_bTime.getTime() - ts_bTime.getTime())/1000;
                        yejian = (ts_eTime.getTime() - ts_set_bTime.getTime())/1000;
                    } else {
                        //开始时间在白天 - 结束时间在白天
                        baitian = (ts_eTime.getTime() - ts_bTime.getTime())/1000;
                        yejian = 0;
                    }
                }
            } else {
                for (int i = 0; i <= count; i++) {
                    if (i == 0) {
                        if (b_inNight) {
                            //开始时间在夜间
                            if (ts_bTime.getTime() >= ts_set_bTime.getTime() && ts_bTime.getTime() <= HOUR.parse("23:59:59").getTime()) {
                                baitian += 0;
                                yejian += (HOUR.parse("23:59:59").getTime() - ts_bTime.getTime())/1000 + second;
                            } else if (ts_bTime.getTime() >= HOUR.parse("00:00:00").getTime() && ts_bTime.getTime() <= ts_set_eTime.getTime()) {
                                baitian += (ts_set_bTime.getTime() - ts_set_eTime.getTime())/1000;
                                yejian += (HOUR.parse("23:59:59").getTime() - ts_set_bTime.getTime())/1000 + second;
                                yejian += (ts_set_eTime.getTime() - ts_bTime.getTime())/1000;
                            }
                        } else {
                            //开始时间在白天
                            baitian += (ts_set_bTime.getTime() - ts_bTime.getTime())/1000;
                            yejian += (HOUR.parse("23:59:59").getTime() - ts_set_bTime.getTime())/1000 + second;
                        }
                    } else if (i == count) {
                        if (e_inNight) {
                            //结束时间在夜间
                            if (ts_eTime.getTime() >= ts_set_bTime.getTime() && ts_eTime.getTime() <= HOUR.parse("23:59:59").getTime()) {
                                baitian += (ts_set_bTime.getTime() - ts_set_eTime.getTime())/1000;
                                yejian += (ts_eTime.getTime() - ts_set_bTime.getTime())/1000;
                                yejian += (ts_set_eTime.getTime() - HOUR.parse("00:00:00").getTime())/1000;
                            } else if (ts_eTime.getTime() >= HOUR.parse("00:00:00").getTime() && ts_eTime.getTime() <= ts_set_eTime.getTime()) {
                                baitian += 0;
                                yejian += (ts_eTime.getTime() - HOUR.parse("00:00:00").getTime())/1000;
                            }
                        } else {
                            //结束时间在白天
                            baitian += (ts_eTime.getTime() - ts_set_eTime.getTime())/1000;
                            yejian += (ts_set_eTime.getTime() - HOUR.parse("00:00:00").getTime())/1000;
                        }
                    } else {
                        baitian += (ts_set_bTime.getTime() - ts_set_eTime.getTime())/1000;
                        yejian += (HOUR.parse("23:59:59").getTime() - ts_set_bTime.getTime())/1000 + second;
                        yejian += (ts_set_eTime.getTime() - HOUR.parse("00:00:00").getTime())/1000;
                    }
                }
            }
            return MutableTriple.of(true,baitian,yejian);
        } catch (Exception ex) {
            baitian = 0;
            yejian = 0;
            return MutableTriple.of(false,baitian,yejian);
        }
    }


    public static void main(String[] args) {
        System.out.println(getHour("2023-10-10 10:10:10"));
    }

}
